//
//  WNNavView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit

class WNNavView: UIView {
    /// 返回按钮
    lazy var leftBtn: UIButton = {
        let btn = UIButton(frame: .zero)
        btn.setImage(UIImage.init(named: "nav_back"), for: .normal)
        btn.imageView?.contentMode = .scaleAspectFit
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.titleLabel?.textAlignment = .left
        btn.addTarget(self, action: #selector(wn_leftBtnMethod), for: .touchUpInside)
        return btn
    }()
    
    /// 头部标题
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 18)
        label.textColor = .black
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()
    
    lazy var bookView: WNNavBookView = {
        let view = WNNavBookView()
        view.isHidden = true
        return view
    }()
    
    /// 右边按钮
    lazy var rightBtn: UIButton = {
        let btn = UIButton(frame: .zero)
        btn.setImage(nil, for: .normal)
        btn.imageView?.contentMode = .scaleAspectFit
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.titleLabel?.textAlignment = .right
        btn.addTarget(self, action: #selector(wn_rightBtnMethod), for: .touchUpInside)
        btn.isHidden = true
        return btn
    }()
    
    /// audio按钮
    lazy var audioBtn: UIButton = {
        let btn = UIButton(frame: .zero)
        btn.rx.tap.bind { [weak self] in
            self?.audioClosure?()
        }.disposed(by: rx.disposeBag)
        btn.isHidden = true
        return btn
    }()
    
    lazy var marginView: UIView = {
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: ScreenW, height: NavMargin))
        view.backgroundColor = .clear
        return view
    }()
    
    /// 更改头标题属性
    var titleName: String? {
        didSet {
            titleLabel.isHidden = false
            titleLabel.text = titleName
        }
    }
    /// 更改头标题颜色
    var titleColor: UIColor? {
        didSet {
            titleLabel.textColor = titleColor
        }
    }
    /// 左边的按钮是否显示
    var ishiddLeftBtn: Bool = false {
        didSet {
            leftBtn.isHidden = ishiddLeftBtn
        }
    }
    /// 更改左边按钮的图片
    var leftBtnImage: String? {
        didSet {
            leftBtn.setImage(UIImage.init(named: leftBtnImage ?? ""), for: .normal)
            leftBtn.setTitle(nil, for: .normal)
            
        }
    }
    /// 更改左边按钮的标题
    var leftBtnTitle: String? {
        didSet {
            leftBtn.setImage(nil, for: .normal)
            leftBtn.setTitle(leftBtnTitle, for: .normal)
        }
    }
    /// 右边的按钮是否显示
    var ishiddRightBtn: Bool = true {
        didSet {
            rightBtn.isHidden = ishiddRightBtn
        }
    }
    /// 更改右边按钮的图片
    var rightBtnImge: String? {
        didSet {
            rightBtn.setImage(UIImage.init(named: rightBtnImge ?? ""), for: .normal)
            rightBtn.setTitle(nil, for: .normal)
            rightBtn.isHidden = false
            
        }
    }
    /// 更改右边按钮的标题
    var rightBtnTitle: String? {
        didSet {
            rightBtn.setImage(nil, for: .normal)
            rightBtn.setTitle(rightBtnTitle, for: .normal)
            rightBtn.isHidden = false
        }
    }
    
    var bookDetailModel: BookDetailModel? {
        didSet {
            bookView.isHidden = bookDetailModel == nil
            bookView.model = bookDetailModel
        }
    }
    
    var leftClosure: ReturnClosure?
    
    var rightClosure: ReturnClosure?
       
    var isBegin = false

    var isPlay = false

    var audioClosure: ReturnClosure?

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(marginView)
        addSubview(titleLabel)
        addSubview(leftBtn)
        addSubview(rightBtn)
        addSubview(audioBtn)
        addSubview(bookView)
        marginView.snp.makeConstraints { make in
            make.top.left.right.equalTo(0)
            make.height.equalTo(StatusBarHeight)
        }
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-11)
            make.height.equalTo(22)
        }
        leftBtn.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.centerY.equalTo(titleLabel.snp.centerY)
            make.width.height.equalTo(32)
        }
        rightBtn.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.centerY.equalTo(titleLabel.snp.centerY)
            make.height.equalTo(32)
        }
        audioBtn.snp.makeConstraints { make in
            make.right.equalTo(rightBtn.snp.left).offset(-12)
            make.centerY.equalTo(titleLabel.snp.centerY)
            make.height.width.equalTo(32)
        }
        bookView.snp.makeConstraints { make in
            make.centerY.equalTo(titleLabel.snp.centerY)
            make.left.equalTo(80)
            make.size.equalTo(CGSize.init(width: 170, height: 36))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func wn_leftBtnMethod() {
        leftClosure?()
    }
    
    @objc func wn_rightBtnMethod() {
        rightClosure?()
    }
    
    func darkModeConfig() {
        backgroundColor = isDark() ? .init(named: "black_121212") : .white
        leftBtnImage = isDark() ? "nav_back_night" : "nav_back"
        titleLabel.textColor = isDark() ? .init(named: "gray_666666") : .black
        audioBtn.setImage(UIImage(named: isDark() ? "read_audo_night" : "read_audo"), for: .normal)
    }
}
